package service;

import entity.Attribute;
import entity.Type;
import entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class IUserServiceImpl implements IUserService {
    @Autowired
    private MongoTemplate mongoTemplate;

    public void registerUser(User user) {

        System.out.println(mongoTemplate);

        mongoTemplate.insert(user);
    }

    public User userLogin(User user) {
        Query query = new Query();
        Criteria name = new Criteria("name").is(user.getName()).and("password").is(user.getPassword());
        query.addCriteria(name);
        return mongoTemplate.findOne(query,User.class);
    }

    public void updateAttribute(String id, Attribute attribute) throws IllegalAccessException {
        Query query = new Query();
        User user = mongoTemplate.findById(id,User.class);
        List<Attribute> attributes = user.getAttributes();
        for (int i = 0; i < attributes.size(); i++) {
            Attribute attribute1 = attributes.get(i);
            if(attribute.getColumn().equals(attribute1.getColumn())){
                Update update = Update.update("attributes.$.value", attribute.getValue());
                mongoTemplate.updateMulti(query,update,User.class);
                return ;
            }
        }
        //否则添加新属性
        query = new Query(Criteria.where("id").is(id));
        Update update=new Update();
        update.addToSet("attributes",attribute);
        mongoTemplate.updateMulti(query,update,User.class);
    }
    public void deleteAttribute(String id, String attribute) throws IllegalAccessException {
        Query query = new Query(Criteria.where("id").is(id).and(("attributes.column")).is(attribute));
        User user = mongoTemplate.findById(id, User.class);
        List<Attribute> attributes = user.getAttributes();
        for(int i=0;i<attributes.size();i++){
            Attribute attr=attributes.get(i);
            if(attribute.equals(attr.getColumn())){
                Update update=new Update();
                update.pull("attributes", attr);
                mongoTemplate.updateMulti(query,update,User.class);
                return ;
            }
        }
    }

    public void deleteUserById(String id) {
        Query query=new Query();
        Criteria criteria=new Criteria("id").is(id).and("type").is(Type.QQ);
        query.addCriteria(criteria);
        mongoTemplate.remove(query,User.class);
    }

    public void deleteUserByAttribute(Attribute... attributes) {
        Query query=new Query();
        query.addCriteria(new Criteria("type").is(Type.QQ));

        Criteria criteria= new Criteria("column");
        for (Attribute attribute : attributes) {
            if(attribute.getValue().getClass().getSimpleName().equals("String")) {
                criteria.is(attribute.getColumn()).and("value").regex(attribute.getValue().toString());
            }else{
                criteria.is(attribute.getColumn()).and("value").is(attribute.getValue());
            }
        }
        Criteria c=new Criteria("attributes");
        c.elemMatch(criteria);
        query.addCriteria(c);

        mongoTemplate.remove(query,User.class);
    }
    public User queryUserById(String id) {
        return mongoTemplate.findById(id,User.class);
    }

    public List<User> queryByAttributes(int pageNow, int pageSize, Attribute... attributes) {
        Query query=new Query();
        query.addCriteria(new Criteria("type").is(Type.QQ));
        query.skip((pageNow-1)*pageSize);
        query.limit(pageSize);

        Criteria criteria= new Criteria("column");
        for (Attribute attribute : attributes) {
            if(attribute.getValue().getClass().getSimpleName().equals("String")) {
                criteria.is(attribute.getColumn()).and("value").regex(attribute.getValue().toString());
            }else{
                criteria.is(attribute.getColumn()).and("value").is(attribute.getValue());
            }
        }
        Criteria c=new Criteria("attributes");
        c.elemMatch(criteria);
        query.addCriteria(c);
        return mongoTemplate.findAll(User.class);
    }

    public int queryAll() {
        List<User> list = mongoTemplate.findAll(User.class);
        return list.size();
    }
}
